/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sgsmcan.Dao;

import com.sgsmcan.Entidad.Almacen;
import com.sgsmcan.Hibernate.HibernateUtil;
import com.sgsmcan.Utils.Utilitario;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author GOMEZ
 */
public class AlmacenDaoImp implements AlmacenDao {

    @Override
    public void insertar(Almacen almacen) {
        Session session = null;
        Transaction tx = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            tx = session.beginTransaction();
            session.save(almacen);
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            //  Utilitario.LOG.error(CLASE + " Error al insertar ", e);
            throw new HibernateException(e);
        } finally {
            if (session != null) {
                session.close();
            }

        }
    }

    @Override
    public void eliminar(Almacen almacen) {
        Session session = null;
        Transaction tx = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            tx = session.beginTransaction();
            session.delete(almacen);
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            //  Utilitario.LOG.error(CLASE + " Error al insertar ", e);
            throw new HibernateException(e);
        } finally {
            if (session != null) {
                session.close();
            }

        }
    }

    @Override
    public void modificar(Almacen almacen) {
        Session session = null;
        Transaction tx = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            tx = session.beginTransaction();
            session.update(almacen);
            tx.commit();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            //  Utilitario.LOG.error(CLASE + " Error al insertar ", e);
            throw new HibernateException(e);
        } finally {
            if (session != null) {
                session.close();
            }

        }
    }

    @Override
    public Almacen getAlmacenId(Integer id) {
        Session session = null;
        try{
            session = HibernateUtil.getSessionFactory().openSession();
            Criteria criteria = session.createCriteria(Almacen.class,"almacen")
                    .createAlias("almacen.producto","prod").add(Restrictions.eq("almacen.id", id));
            return (Almacen)criteria.uniqueResult();
        }catch(HibernateException e){
            Utilitario.LOG.error("Error en AlmacenDaoImp", e);
            throw  new HibernateException(e);
        }finally{ 
            if(session != null){
                session.close();
            }
        }
    }

    @Override
    public DetachedCriteria getAlmacen() {
        DetachedCriteria criteria = DetachedCriteria.forClass(Almacen.class, "almacen").createAlias("almacen.producto", "prod");
        return criteria;
    }

}
